package com.example.todolist;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity {

	// Members : menu
	private static final int DELETE_WORKBOOK = Menu.FIRST+2;
	private static final int INSERT_WORK = Menu.FIRST ;
	private static final int ABOUT_US = Menu.FIRST + 4;
	
	ArrayList<Work> m_arl_work;
	ListWorkAdapter m_lsw_adapter;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		m_arl_work = new ArrayList<Work>();
		m_lsw_adapter = new ListWorkAdapter(this, R.layout.list, m_arl_work);
		
		final EditText v_txt_work = (EditText) findViewById(R.id.m_txt_work_enter);
		final EditText v_txt_hour = (EditText) findViewById(R.id.m_txt_hour_edit);
		final EditText v_txt_minute = (EditText) findViewById(R.id.m_txt_minute_edit);
		final EditText v_txt_dia_diem = (EditText) findViewById(R.id.m_txt_dia_diem_edit);
		
		final Button v_cmd_add = (Button) findViewById(R.id.m_cmd_enter);
		final ListView v_lst_view = (ListView) findViewById(R.id.m_lsv_list_todo);
		
		v_lst_view.setAdapter(m_lsw_adapter);
		
		OnClickListener v_add = new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(v_txt_work.getText().toString().equals("")
						|| v_txt_hour.getText().toString().equals("")
						|| v_txt_minute.getText().toString().equals("")
						|| v_txt_dia_diem.getText().equals(""))
				{
					AlertDialog.Builder v_alert = new AlertDialog.Builder(MainActivity.this);
					v_alert.setTitle("Missing Infor");
					v_alert.setMessage("Please enter all the work information!");
					v_alert.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							
						}
					});
					v_alert.show();
				}
				else 
				{
					String v_str_work_content = v_txt_work.getText().toString();
					String v_str_time = v_txt_hour.getText().toString() + ":" 
					+ v_txt_minute.getText().toString();
					String v_str_dia_diem = v_txt_dia_diem.getText().toString();
					Work v_work = new Work(v_str_work_content, v_str_time,v_str_dia_diem);
					m_arl_work.add(v_work);
					// Dòng này refresh lại listivew; dựa vào listwork Adapter thoogn báo cho listview biết vừa có thêm 1 item đc thêm vào
					m_lsw_adapter.notifyDataSetChanged();
					v_txt_work.setText("");
					v_txt_hour.setText("");
					v_txt_minute.setText("");
					v_txt_dia_diem.setText("");
				}
			}
		};
		v_cmd_add.setOnClickListener(v_add);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, INSERT_WORK, 0,"Insert")
		.setIcon(android.R.drawable.ic_input_add);
		menu.add(0, DELETE_WORKBOOK, 0, "Delete")
		.setIcon(android.R.drawable.ic_delete);
		menu.add(0, ABOUT_US, 0, "About us")
		.setIcon(android.R.drawable.ic_menu_info_details);
		return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId()) {
		case INSERT_WORK:
			insert_work();
		case DELETE_WORKBOOK:
			delete_checked_work();
			break;
		case ABOUT_US:
			AlertDialog.Builder v_builder = new AlertDialog.Builder(this);
			v_builder.setTitle("BKIndex Android");
			v_builder.setMessage("AUTHOR: Dinh Hong Linh \n SOURCE: bkindex.com");
			v_builder.setPositiveButton("Close", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			});
			v_builder.setIcon(android.R.drawable.ic_dialog_info);
			v_builder.show();
			break;
		}
		return true;
	}

	private void insert_work()
	{
		// TODO Auto-generated method stub
		final EditText v_txt_work = (EditText) findViewById(R.id.m_txt_work_enter);
		final EditText v_txt_hour = (EditText) findViewById(R.id.m_txt_hour_edit);
		final EditText v_txt_minute = (EditText) findViewById(R.id.m_txt_minute_edit);
		final EditText v_txt_dia_diem = (EditText) findViewById(R.id.m_txt_dia_diem_edit);
		
		final Button v_cmd_add = (Button) findViewById(R.id.m_cmd_enter);
		final ListView v_lst_view = (ListView) findViewById(R.id.m_lsv_list_todo);
		
		v_lst_view.setAdapter(m_lsw_adapter);
		if(v_txt_work.getText().toString().equals("")
				|| v_txt_hour.getText().toString().equals("")
				|| v_txt_minute.getText().toString().equals(""))
		{
			AlertDialog.Builder v_alert = new AlertDialog.Builder(MainActivity.this);
			v_alert.setTitle("Missing Infor");
			v_alert.setMessage("Please enter all the work information!");
			v_alert.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			});
			v_alert.show();
		}
		else 
		{
			String v_str_work_content = v_txt_work.getText().toString();
			String v_str_time = v_txt_hour.getText().toString() + ":" 
			+ v_txt_minute.getText().toString();
			String v_str_dia_diem = v_txt_dia_diem.getText().toString();
			Work v_work = new Work(v_str_work_content, v_str_time, v_str_dia_diem);
			m_arl_work.add(v_work);
			m_lsw_adapter.notifyDataSetChanged();
			v_txt_work.setText("");
			v_txt_hour.setText("");
			v_txt_minute.setText("");
			v_txt_dia_diem.setText("");
		}
	}
	private void delete_checked_work() {
		if(m_arl_work.size() > 0)
		{
			for (int v_i = 0; v_i < m_arl_work.size(); v_i++) {
				if(v_i > m_arl_work.size()) break;
				
				if(m_arl_work.get(v_i).isChecked())
				{
					m_arl_work.remove(v_i);
					// Khi có bất kỳ sự thay đổi nào từ arraywork, ta sẽ dùng listworkAdapter thông báo cho listview (thông qua hàm notifyDatasetChange)
					m_lsw_adapter.notifyDataSetChanged();
					// Về bản chất, listview chỉ là cái lưới hiển thị dữ liệu;
					 // ArrayWork là dataset
					// ListworkAdapter là cái cầu nối;
					continue;
				}
			}
		}
	}
}
